import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/my_constant.dart';
import 'dart:convert';

class QuestionsViewModel extends BaseViewModel {
  List<FaqModel> dataList = [];

  // 请求数据
  @override
  Future<void> requestData({Function(dynamic)? onSuccess, Function(dynamic)? onError}) async {
    setState(ViewState.loading);
    // 去请求服务器数据.
    Map<String, dynamic> resultMap = await DioHttpManager()
        .request(MyApi.getCommonProblem, method: DioHttpManager.post, data: {"limit": MyConstant.pageSize});
    if (resultMap['code'] == 0) {
      // 请求成功, 解析数据.
      List<dynamic> resultList = resultMap['data'];
      // 遍历数据, 添加到列表中.
      for (var element in resultList) {
        // 创建FAQ对象, 并添加到列表中.
        FaqModel faqModel = FaqModel.fromMap(element);
        dataList.add(faqModel);
      }
    }
    // 更新页面.
    setState(ViewState.normal);
  }
}


class FaqModel {
  final int? id;
  final int? appType;
  final String? title;
  final String? desc;
  final String? img;
  final String? createTime;
  bool isExpanded;

  FaqModel({
    this.id,
    this.appType,
    this.title,
    this.desc,
    this.img,
    this.createTime,
    this.isExpanded = false,
  });

  factory FaqModel.fromJson(String str) => FaqModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory FaqModel.fromMap(Map<String, dynamic> json) => FaqModel(
        id: json["id"],
        appType: json["app_type"],
        title: json["title"],
        desc: json["desc"],
        img: json["img"],
        isExpanded: json["isExpanded"] ?? false,
        createTime: json["create_time"],
      );

  Map<String, dynamic> toMap() => {
        "id": id,
        "app_type": appType,
        "title": title,
        "desc": desc,
        "img": img,
        "create_time": createTime,
        "isExpanded": isExpanded,
      };
}
